// pages/vision-details/vision-details.js
var app = getApp();
var com = require('../../common.js');
var touchDotX = 0; //X按下时坐标
var touchDotY = 0; //y按下时坐标
var interval; //计时器
var time = 0; //从按下到松开共多少时间*100
Page({
  data: {
    yactive: false,
    jactive: false,
    model: false,
    id: '',
    info: {},
    statusd: 1,
    sss: false,
    name: '',
    onPageDelete: false
  },
  onLoad: function(options) {
    var _this = this
    _this.setData({
      id: options.id,
      name: options.name
    })
    setTimeout(() => {
      _this.setData({
        statusd: 0
      })
    }, 1500)
  },
  toggle: function(e) {
    if (e.currentTarget.dataset.text == "远") {
      this.setData({
        yactive: !this.data.yactive
      })
    }
    if (e.currentTarget.dataset.text == "近") {
      this.setData({
        jactive: !this.data.jactive
      })
    }
  },
  showModel: function() {
    this.setData({
      model: true
    })
  },
  hideModel: function() {
    this.setData({
      model: false
    })
  },
  onReady: function() {},
  onShow: function() {
    var _this = this
    _this.onInfo()
  },
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  // onShareAppMessage: function() {},
  onInfo() {
    var _this = this
    com.link(app.link.optometryList, {
      user: _this.data.id
    }, function(res) {
      console.log(res);
      if (res.data.data.length != 0) {
        com.link(app.link.optometrys + res.data.data[0].id + '.st', {
          user: _this.data.id
        }, function(res) {
          console.log(res);
          _this.setData({
            info: res.data.data
          })
        }, "GET")
        res.data.data.map((v, k) => {
          console.log(res.data.data[k])
          res.data.data[k].click = false
          res.data.data[k].onPageDelete = false
          res.data.data[k].ri = res.data.data[k].create_time.substring(8, 10)
          res.data.data[k].yue = res.data.data[k].create_time.substring(5, 7)
          res.data.data[k].nian = res.data.data[k].create_time.substring(0, 4)
        })
        _this.setData({
          list: res.data.data
        })
      }
    }, "GET")
  },
  onTime(e) {
    var _this = this,
      list = _this.data.list
    list.map((v, k) => {
      if (list[k].id == e.currentTarget.id) {
        if (list[k].click) {
          list[k].click = false
          list[k].onPageDelete = false
        } else {
          list[k].click = true
          list[k].onPageDelete = false
        }
      } else {
        list[k].click = false
        list[k].onPageDelete = false
      }
    })
    com.link(app.link.optometrys + e.currentTarget.id + '.st', {
      user: _this.data.id
    }, function(res) {
      console.log(res);
      _this.setData({
        info: res.data.data,
        list: list,
        model: false
      })
    }, "GET")
  },
  jampPage() {
    var _this = this
    wx.navigateTo({
      url: "/pages/onCreate/onCreate?id=" + _this.data.id + '&name=' + _this.data.name,
    })
  },
  // 触摸开始事件
  touchStart: function(e) {
    touchDotX = e.touches[0].pageX; // 获取触摸时的原点
    touchDotY = e.touches[0].pageY;
    // 使用js计时器记录时间    
    interval = setInterval(function() {
      time++;
    }, 100);
  },
  touchmove() {
    var _this = this
    _this.setData({
      onPageDelete: true
    })
  },
  // 触摸结束事件
  touchEnd: function(e) {
    var onPageDelete = this.data.onPageDelete
    if (onPageDelete) {
      let touchMoveX = e.changedTouches[0].pageX;
      let touchMoveY = e.changedTouches[0].pageY;
      let tmX = touchMoveX - touchDotX;
      let tmY = touchMoveY - touchDotY;
      var _this = this,
        list = _this.data.list
      if (time < 20) {
        let absX = Math.abs(tmX);
        let absY = Math.abs(tmY);
        if (absX > 2 * absY) {
          if (tmX < 0) {
            list.map((v, k) => {
              if (list[k].id == e.currentTarget.id) {
                list[k].onPageDelete = true
                list[k].click = false
              }
            })
            _this.setData({
              list: list,
              onPageDelete: false
            })
            console.log("左滑")
          } else {
            list.map((v, k) => {
              if (list[k].id == e.currentTarget.id) {
                list[k].onPageDelete = false
                list[k].click = false
              }
            })
            _this.setData({
              list: list,
              onPageDelete: false
            })
            console.log("右滑")
          }
        }
      }
      clearInterval(interval); // 清除setInterval
      time = 0;
    } else {
      clearInterval(interval); // 清除setInterval
      time = 0;
    }
  }
})